/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2021-01-04
*/
package com.rzico.article.service;

import com.rzico.article.model.RecommendVo;
import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.article.entity.Recommend;
import com.rzico.basics.model.MapPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.article.mapper.RecommendMapper;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 推荐平台业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class RecommendService extends BaseServiceImpl<Recommend, String> {

    @Autowired
    private RecommendMapper recommendMapper;

    @Override
    public BaseMapper<Recommend, String> getMapper() {
        return recommendMapper;
    }


    public List<RecommendVo> selectLbsList(Map<String, Object> params,Double lat,Double lng) {

        List<RecommendVo> shopList = recommendMapper.selectLbsList(params);
        if (lat!=null && lng !=null) {
            for (RecommendVo shopVo : shopList) {
                if (shopVo.getLat() != null && shopVo.getLng() != null) {
                    MapPoint point = new MapPoint();
                    point.setLng(lng);
                    point.setLat(lat);

                    MapPoint shopPoint = new MapPoint();
                    shopPoint.setLat(shopVo.getLat());
                    shopPoint.setLng(shopVo.getLng());

                    Double d = point.distatce(shopPoint);
                    shopVo.setDistance(d);
                }
            }
            //开始排序
            Collections.sort(shopList, new Comparator<RecommendVo>() {
                public int compare(RecommendVo n1, RecommendVo n2) {
                    if (n1.getDistance()==null) {
                        return -1;
                    }
                    if (n2.getDistance()==null) {
                        return 1;
                    }
                    return n1.getDistance().compareTo(n2.getDistance());
                }
            });
        }

        return shopList;
    }



}
